package com.lungle.warmdiary.app.view.active.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.utils.LoadLocalImageUtil;
import com.lungle.warmdiary.app.utils.StringUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CommentDetailAdapter extends BaseAdapter {
    private final int MAX_UPLOAD = 10;
    private List<String> list;
    private Context mContext;

    public CommentDetailAdapter(Context context, List<String> list) {
        this.list = list;
        this.mContext = context;
    }


    public int getRealCount() {
        return list.size();
    }

    @Override
    public int getCount() {
        if (list.size() < MAX_UPLOAD) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }


    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //        ViewHolder vh;
        //        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.item_picture_upload, parent, false);
        //            vh = new ViewHolder(convertView);
        //            convertView.setTag(vh);
        //        } else {
        //            vh = (ViewHolder) convertView.getTag();
        //        }

        ImageView imageView = (ImageView) layout.findViewById(R.id.iv_active_img);

        if (list.size() < MAX_UPLOAD) {
            if (position < list.size()) {
                loadImage(list.get(position), imageView);
            } else {
                imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_default_camera));

            }
        } else {
            loadImage(list.get(position), imageView);
        }


        return layout;
    }


    private void loadImage(String url, ImageView imageView) {
        if (url.startsWith("http")) {
            ImageLoader.getInstance().loadImage(StringUtils.getPreviewImageUrl(url), new ImageSize(100, 100), new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    imageView.setImageBitmap(loadedImage);
                }
            });
        } else {
            LoadLocalImageUtil.getInstance().displayFromSDCard(url, imageView);
        }
    }

    static class ViewHolder {
        @Bind(R.id.iv_active_img)
        ImageView mIvActive;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}